//
//  HMStatusViewModel.swift
//  Weibo12
//
//  Created by HM on 15/12/31.
//  Copyright © 2015年 itheima. All rights reserved.
//  cell 对应的视图模型，

import UIKit

class HMStatusViewModel: NSObject {
    // 当前视图模型对应的模型数据
    var status: HMStatus? {
        didSet{
            
            
            // 计算会员要显示的图片
            if let mbrank = status?.user?.mbrank where mbrank > 0 && mbrank < 7 {
                memberImage = UIImage(named: "common_icon_membership_level\(mbrank)")
            }else{
                // 设置会员图标
                memberImage = nil
            }
            // 设置认证的图标
            verifiedImage = calcVerifiedImage()
            
            // 转发评论赞要显示的字符串
            repostsCount = calcStatusCount(status?.reposts_count ?? 0, defaultTitle: "转发")
            commentsCount = calcStatusCount(status?.comments_count ?? 0, defaultTitle: "评论")
            attitudesCount = calcStatusCount(status?.attitudes_count ?? 0, defaultTitle: "赞")
            
            
            // 判断是否有转发微博
            if let retweetText = status?.retweeted_status?.text {
                if let name = status?.retweeted_status?.user?.screen_name {
                    retweetStatusText = "@\(name):\(retweetText)"
                }
            }
        }
    }
    
    // 会员显示的图片
    var memberImage: UIImage?
    // 认证图片
    var verifiedImage: UIImage?
    
    // 转发评论赞的数量字符串
    var repostsCount: String?
    var commentsCount: String?
    var attitudesCount: String?
    
    /// 转发微博的内容(如果有转发微博的话，这个属性才有意义)
    var retweetStatusText: String? // @昵称: 转发微博的内容
    
    
    /// 请算转发市评论赞的数量字符串
    ///
    /// - parameter count:        <#count description#>
    /// - parameter defaultTitle: 如果 count 等于0的话，就显示 defaultTitle
    ///
    /// - returns: <#return value description#>
    private func calcStatusCount(count: Int, defaultTitle: String) -> String {
        
        /**
            - 比如转发数量为 9990 --> 9990
            - 大于万 11111 --> 1.1 万
            - 大于万 10900 --> 1万
        */
        // 计算显示什么字符串

        // let count = status?.reposts_count ?? 0
        
        if count == 0 {
            return defaultTitle
        }
        
        if count < 10000 {
            return "\(count)"
        }else{
            // 先除以1000
            let result = CGFloat(count / 1000) / 10
            let resultStr = "\(result)万"
            if resultStr.containsString(".0"){
                return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return resultStr
        }
    }
    
    /// 计算认证的图标
    private func calcVerifiedImage() -> UIImage? {
        // 计算认证图标
        if let verifiedType = status?.user?.verified_type {
            switch verifiedType {
            case -1:
               return nil
            case 0:
                return UIImage(named: "avatar_vip")
            case 2,3,5:
                return UIImage(named: "avatar_enterprise_vip")
            case 220:
                return UIImage(named: "avatar_grassroot")
            default:
                return nil
            }
        }
        return nil
    }
    
    
    override var description: String {
        let keys = ["status"]
        return dictionaryWithValuesForKeys(keys).description
    }
}
